import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const themeStore = defineStore(
    'theme',
    () => {
        // 主题类名 只支持 light 和 dark
        const themeClass = ref<any>('light')

        const setNavigationBarColorHandler = () => {
            // 这里主要是改变tabbar和导航栏的颜色 
            const darkNavigationBar = {
                frontColor: '#ffffff',
                backgroundColor: '#000000',
                animation: {
                    duration: 0,
                    timingFunc: 'easeIn' as any
                }
            }

            const lightNavigationBar = {
                frontColor: '#000000',
                backgroundColor: '#ffffff',
                animation: {
                    duration: 0,
                    timingFunc: 'easeIn' as any
                }
            }

            const tabBarStyle = {
                color: themeClass.value === 'dark' ? '#ffffff' : '#000000',
                // selectedColor: themeClass.value === 'dark' ? '#ed1941' : '#0577f6',
                backgroundColor: themeClass.value === 'dark' ? '#000000' : '#ffffff'
            }

            uni.setNavigationBarColor((themeClass.value === 'dark') ? darkNavigationBar : lightNavigationBar)
            //  TabBar 页面路径
            const tabBarPages = [
                'pages/index/index',
                'pages/category/category',
                'pages/my/my'
            ]

            // 判断当前页面是否是 TabBar 页面
            const currentPages = getCurrentPages()
            const currentPage = currentPages[currentPages.length - 1]
            if (tabBarPages.includes(currentPage.route!)) {
                uni.setTabBarStyle(tabBarStyle)
            }
        }

        return {
            themeClass,
            setNavigationBarColorHandler
        }
    },
    // TODO: 持久化
    {
        // 网页端
        // persist: true,
        // 小程序配置
        persist: {
            storage: {
                getItem(key) {
                    return uni.getStorageSync(key);
                },
                setItem(key, value) {
                    uni.setStorageSync(key, value);
                },
            }
        }
    },
)
